package com.xx.jicheng.controller.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.xx.jicheng.service.CategoryService;

@Controller
public class AdminProductQueryController {
	@Autowired
	private HttpSolrClient client;
	
	@Autowired
	private CategoryService categoryService;
	
	@RequestMapping(path = "/query/all")
	public String all(@RequestParam(defaultValue = "0") int start, 
					  @RequestParam(defaultValue = "10") int rows,
					  String q,
					  @RequestParam(value="fq", required=false) String[] fq,
					  Model model,HttpServletRequest request) throws SolrServerException, IOException {
		SolrQuery query = new SolrQuery();
		query.set("facet", "on");		
		query.set("facet.mincount", 1);
		query.set("facet.limit", 11);		
		query.set("start", start);
		query.set("rows", rows);
		query.set("q", "*:*");
		query.set("facet.field", "c1");
		model.addAttribute("path", request.getContextPath()+"/query/c1");
		QueryResponse resp = client.query(query);
		model.addAttribute("query", query);
		model.addAttribute("resp", resp);
		return "all";
	}

	@RequestMapping(path = "/query/c1/{v}")
	public String query(@PathVariable("v") String v,
					  @RequestParam(defaultValue = "0") int start, 
					  @RequestParam(defaultValue = "10") int rows,
					  @RequestParam(value="fq", required=false) String[] fq,
					  Model model,HttpServletRequest request) throws SolrServerException, IOException {
		SolrQuery query = new SolrQuery();
		query.set("facet", "on");		
		query.set("facet.mincount", 1);
		query.set("facet.limit", 11);		
		query.set("start", start);
		query.set("rows", rows);
		query.set("q", "c1:" + v);
		// 包含fq条件名的集合，对于已经作为查询条件的名称，最后显示时应当忽略
		Set<String> conditions = new HashSet<>();
		StringBuilder prevfq = new StringBuilder(128);
		String c2 = null;
		String c3 = null;
		if(fq != null){
			query.addFilterQuery(fq);
			for (String f:fq){
				String[] cond = f.split(":");
				conditions.add(cond[0]);
				if(cond[0].equals("c2")) {
					c2 = cond[1];
				}
				if(cond[0].equals("c3")) {
					c3 = cond[1];
				}
				prevfq.append("&fq=").append(f);
			}
		}
		// 如果已经有二级类别的条件，在facet 应当添加三级类别
		if(conditions.contains("c3")){
//			System.out.println(categoryService.findAttrs(c3).values());
			List<String> all = new ArrayList<>();
			all.add("brand");
			for(String attr: categoryService.findAttrs(c3).values()){
				all.add("attr_" + attr);
			}
			query.addFacetField(all.toArray(new String[]{}));
		} else if(conditions.contains("c2")){
//			System.out.println(categoryService.findAttrs(c2).values());
			List<String> all = new ArrayList<>();
			all.add("c3");
			all.add("brand");
			for(String attr: categoryService.findAttrs(c2).values()){
				all.add("attr_" + attr);
			}
			query.addFacetField(all.toArray(new String[]{}));
		} else {
			query.addFacetField("c2", "brand");
		}
		query.set("facet.range", "price");
		query.set("facet.range.start", 0);
		query.set("facet.range.end", 400000);
		query.set("facet.range.gap", 40000);
		model.addAttribute("path", request.getContextPath()+"/query/c1/"+v);
		QueryResponse resp = client.query(query);
		model.addAttribute("query", query);
		model.addAttribute("resp", resp);
		if(fq != null){
			model.addAttribute("prevfq", prevfq);
		}
		model.addAttribute("exclude", conditions);
		return "query";
	}
}
